//
//  ViewController.m
//  List
//
//  Created by itclimb on 2017/7/31.
//  Copyright © 2017年 itclimb.yuancheng.com. All rights reserved.
//

#import "ViewController.h"
#import "JJFriendGroup.h"
#import "JJFriendListSectionHeadView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) NSMutableArray<JJFriendGroup *> *datas;

@property(nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"列表";
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource&UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    JJFriendGroup *group = self.datas[section];
  
    return group.isSelected?group.friends.count:0;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    JJFriendGroup *group = self.datas[indexPath.section];
    JJFriend *friend = group.friends[indexPath.row];
    cell.textLabel.text = friend.name;
    cell.detailTextLabel.text = friend.intro;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    JJFriendListSectionHeadView *headView = [JJFriendListSectionHeadView friendListSectionHeadViewFromXib];
    JJFriendGroup *group = self.datas[section];
    headView.group = group;
    [headView setSectionHeadViewBlock:^(BOOL isSelected) {
        group.isSelected = isSelected;
        self.datas[section] = group;
        [self.tableView reloadData];
    }];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}


//MARK: - lazy load
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray *)datas{
    if (!_datas) {
        NSArray *dataArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]];
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dataArr.count];
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
            JJFriendGroup *group = [JJFriendGroup friendGroupWithDict:dict];
            [arrM addObject:group];
        }];
        _datas = arrM;
    }
    return _datas;
}

@end
